/**
 * StartFileService.java
 */
package fss;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.Scanner;

import fss.remote.FssServer;
import fss.remote.ServiceRegistry;
import fss.util.rmi.RmiId;
import fss.util.rmi.RmiUtils;

/**
 * This is a command line utility to start and stop a Remote Service.
 * 
 * @author thinh
 *
 */
public class StartFileService {
    /**
     * Application entry.
     * @param args not used
     * @throws RemoteException 
     */
    public static void main(String[] args) throws RemoteException {
        Scanner stdin = new Scanner(System.in);        
        System.out.println("*********************************************************************************************");      
        System.out.println("File Service");
        System.out.println();
        System.out.println("Instructions:");
        System.out.println("1. Enter the Service Registry indentification (service will register to this registry)");
        System.out.println("2. Enter this service identification");
        System.out.println("*********************************************************************************************");
        System.out.print("Hit enter to continue...");
        System.out.println();
        stdin.nextLine();
        
        String host, name, input;
        int port;
        boolean autoresume = false;
        RmiId registryId = null;
        RmiId id = null;
        
        System.out.println("- Service Registry Input -");
        System.out.print("Enter registry host: ");
        host = stdin.nextLine();
        System.out.print("Enter registry port: ");
        port = Integer.parseInt(stdin.nextLine());
        System.out.print("Enter registry name: ");
        name = stdin.nextLine();
        registryId = new RmiId(host, name, port);
        ServiceRegistry registry = (ServiceRegistry)RmiUtils.getRemoteObject(registryId);
        if(registry == null) {
            throw new RemoteException("Attempt to obtain a Service Registry failed at id " + registryId);
        }
        System.out.println();
        
        System.out.println("- Remote Service Input -");
        System.out.print("Enter host: ");
        host = stdin.nextLine();
        System.out.print("Enter port: ");
        port = Integer.parseInt(stdin.nextLine());
        System.out.print("Enter service name: ");
        name = stdin.nextLine();
        System.out.print("Autoresume incomplete transfer(s): (y/n) ");
        input = stdin.nextLine();
        if(input.toLowerCase().charAt(0) == 'y') {
            autoresume = true;
        }
        System.out.println();
        id = new RmiId(host, name, port);
        
        FssServer service = new FssServer(id, registryId, autoresume);
        Registry rmi = RmiUtils.exportFssServer(service);
        registry.register(service);
        service.start();
        
        System.out.println("******************************************************");      
        System.out.println("Hit enter at any time to remove/stop the Service...");
        System.out.println("******************************************************");
        System.out.println();
        stdin.nextLine();

        try {
            service.stop();
            rmi.unbind(id.getName());
        } catch (NotBoundException e) {
            // ignore
        }
        System.out.println("Service ended.");
        System.exit(0);
    }
}
